package com.itheima.service.impl;

import com.alibaba.druid.util.StringUtils;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.constant.MessageConstant;
import com.itheima.dao.PermissionDao;
import com.itheima.entity.PageResult;
import com.itheima.pojo.Permission;
import com.itheima.service.PermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author wuzhouwei
 * @date 2020/4/13
 */
@Service(interfaceClass = PermissionService.class)
@Transactional
public class PermissionServiceImpl implements PermissionService {
    @Autowired
    private PermissionDao permissionDao;


    @Override
    public List<Permission> findAll() {
        return permissionDao.findAll();
    }

    @Override
    public PageResult findPage(Integer currentPage, Integer pageSize, String queryString) {
        List<Permission> permissionList = null;
        if (StringUtils.isEmpty(queryString)) {
            PageHelper.startPage(currentPage, pageSize);
            permissionList = permissionDao.findAll();
        } else {
            PageHelper.startPage(currentPage, pageSize);
            permissionList = permissionDao.findByCondition(queryString);
        }
        //PageInfo只能处理List,  把得到的set集合结果,转成List
        PageInfo<Permission> pageInfo = new PageInfo<Permission>(permissionList);
        return new PageResult(pageInfo.getTotal(), pageInfo.getList());
    }

    @Override
    public Permission findByPermissionId(int permissionId) {
        return permissionDao.findByPermissionId(permissionId);
    }

    @Override
    public void delete(Integer id) {
        int count = permissionDao.findRoleCountByPermissionId(id);
        if (count > 0) {
            throw new RuntimeException(MessageConstant.DELETE_PERMISSION_ASSOCIATE_ROLE_FAIL);
        }
        permissionDao.deleteByPermissionId(id);
    }

    @Override
    public void editPermission(Permission permission) {
        if (permission != null) {
            // 已经被角色关联,不可修改
            int count3 = permissionDao.findRoleCountByPermissionId(permission.getId());
            if (count3 > 0) {
                throw new RuntimeException(MessageConstant.EDIT_PERMISSION_ASSOCIATE_ROLE_FAIL);
            }

            // 已经存在该keyword,不可修改
            int count1 = permissionDao.findPermissionCountByKeywordAndId(permission);
            if (count1 > 0) {
                throw new RuntimeException(MessageConstant.SET_PERMISSION_KEYWORD_FAIL);
            }

            // 已经存在该name,不可修改
            int count2 = permissionDao.findPermissionCountByNameAndId(permission);
            if (count2 > 0) {
                throw new RuntimeException(MessageConstant.SET_PERMISSION_NAME_FAIL);
            }

            permissionDao.editPermission(permission);
        }
    }

    @Override
    public void addPermission(Permission permission) {
        if (permission != null) {
            int count1 = permissionDao.findPermissionCountByName(permission.getName());
            if (count1 > 0) {
                throw new RuntimeException(MessageConstant.ADD_PERMISSION_NAME_FAIL);
            }

            int count2 = permissionDao.findPermissionCountByKeyword(permission.getKeyword());
            if (count2 > 0) {
                throw new RuntimeException(MessageConstant.ADD_PERMISSION_KEYWORD_FAIL);
            }

            permissionDao.addPermission(permission);
        }
    }
}
